/*
 * Copyright (c) 2001-2007, TIBCO Software Inc.
 * Use, modification, and distribution subject to terms of license.
 */

jsx3.require("jsx3.gui.Button");

/**
 * Provides a object-oriented interface for a standard command button, with ActiveMatrix Visual Styles applied to it.
 * <p/>
 * This class publishes the following model events:
 * <ul>
 * <li><code>EXECUTE</code> - when the user clicks on the enabled button, when the button has focus and the user presses the
 *    space or enter key, or when <code>doExecute()</code> is called on the button.</li>
 * <li><code>MENU</code> - on a mouseup event with the right button when the button has a bound context menu.</li>
 * </ul>
 */
jsx3.Class.defineClass("com.gix.gui.Button", jsx3.gui.Button, null, function(Button, Button_prototype) {

    Button.unpressedBG = null;

    /**
     * instance initializer
     * @param strName {String} unique name distinguishing this object from all other JSX GUI objects in the JSX application
     * @param intLeft {int} left position (in pixels) of the object relative to its parent container; not required if button is one of: jsx3.gui.Button.SYSTEMOPEN, jsx3.gui.Button.DIALOGCLOSE, jsx3.gui.Button.DIALOGALPHA, jsx3.gui.Button.DIALOGSHADE
     * @param intTop {int} top position (in pixels) of the object relative to its parent container; not required if button is one of: jsx3.gui.Button.SYSTEMOPEN, jsx3.gui.Button.DIALOGCLOSE, jsx3.gui.Button.DIALOGALPHA, jsx3.gui.Button.DIALOGSHADE
     * @param intWidth {int} width (in pixels) of the object; not required if button is one of: jsx3.gui.Button.SYSTEMOPEN, jsx3.gui.Button.DIALOGCLOSE, jsx3.gui.Button.DIALOGALPHA, jsx3.gui.Button.DIALOGSHADE
     * @param strText {String} text to display in the given button; if null JSXTABLEHEADERCELL.DEFAULTTEXT is used
     */
    Button_prototype.init = function(strName, intLeft, intTop, intWidth, strText) {
        //call constructor for super class
        this.jsxsuper(strName, intLeft, intTop, intWidth, strText);
    };

    /**
    * Gets the background image CSS string for the diabled state.
    *
    * @return {String} the background image CSS string for the disabled state
    */
    Button.getDisabledBG = function() {
        if (!this.disabledBG) {
            this.disabledBG = "background-image: url(" + ux.admin.Main.getServer().resolveURI("jsxaddin://user!LiveCore/images/matrix/disabled_button.jpg") + "); background-repeat: repeat;";
        }
        return this.disabledBG;
    }

    /**
     * Gets the background image CSS string for the unpressed state.
     *
     * @return {String} the background image CSS string for the unpressed state
     */
    Button.getUnpressedBG = function() {
        if (!this.unpressedBG) {
            this.unpressedBG = "background-image: url(" + ux.admin.Main.getServer().resolveURI("jsxaddin://user!LiveCore/images/matrix/buttonBkgrd.jpg") + "); background-repeat: repeat;";
        }
        return this.unpressedBG;
    }

    /**
     * Gets the background image CSS string for the pressed state.
     *
     * @return {String} the background image CSS string for the pressed state
     */
    Button.getPressedBG = function() {
        if (!this.pressedBG) {
            this.pressedBG = "background-image: url(" + ux.admin.Main.getServer().resolveURI("jsxaddin://user!LiveCore/images/matrix/buttonBkgrd_pressed.jpg") + "); background-repeat: repeat;";
        }
        return this.pressedBG;
    }

    Button_prototype.setEnabled = function(intEnabled, bRepaint) {
        if (intEnabled == jsx3.gui.Form.STATEENABLED) {
            this.setDynamicProperty("jsxcolor", "@addin@Btn Color");
            this.setDynamicProperty("jsxborder", "@addin@Btn Border");
            var arr = new Array(1);
            arr[0] = "border";
            this.recalcBox(arr);
            this.setCSSOverride(Button.getUnpressedBG(), true);
        }
        else {
            this.setDynamicProperty("jsxcolor", "@addin@Btn Pressed Color");
            this.setDynamicProperty("jsxborder", "@addin@Btn Disabled Border");
            var arr = new Array(1);
            arr[0] = "border";
            this.recalcBox(arr);
            this.setCSSOverride(Button.getDisabledBG(), true);
        }
        this.getParent().repaint();
        return this.jsxsuper(intEnabled, bRepaint);

    }

    /**
    * Called after a button press.  It changes out the background image to the one for the pressed state, and calls the
    * <code>doUnpressed</code> function 200ms later (to switch the background image back).
    *
    * @param objEvent {Object} the event object
    */
    Button_prototype.doPressed = function(objEvent) {
        //        jsx3.log("Pressed");
        //        for(var name in objEvent.event) {
        //            jsx3.log("Field '" + name + "' has value '" + objEvent.event[name] + "'");
        //        }
        //        if(objEvent.target == this) {
        this.setDynamicProperty("jsxcolor", "@addin@Btn Pressed Color");
        this.setCSSOverride(Button.getPressedBG(), true);

        this.getParent().repaint();
        var me = this;
        setTimeout(function() {
            me.doUnpressed();
        }, 200);

        //        }
    }

    /**
     * Called by the <code>doPressed</code> function (in a timeout), to return a button to the unpressed state background
     * image again.
     */
    Button_prototype.doUnpressed = function() {
        //        jsx3.log("Unpressed");
        if (this.getParent()) {
            this.setDynamicProperty("jsxcolor", "@addin@Btn Color");
            this.setCSSOverride(Button.getUnpressedBG(), true);
            this.getParent().repaint();
        }
    }

    Button_prototype.onSetParent = function(parent) {
        try {
            this.unsubscribeAll();
            var me = this;
            this.subscribe(jsx3.gui.Interactive.EXECUTE, me, me.doPressed);
            //            jsx3.gui.Event.subscribe(jsx3.gui.Event.MOUSEDOWN, me, me.doPressed);
            //            jsx3.gui.Event.subscribe(jsx3.gui.Event.KEYDOWN, me, me.doPressed);
            //            jsx3.gui.Event.subscribe(jsx3.gui.Event.MOUSEUP, me, me.doUnpressed);
            //            jsx3.gui.Event.subscribe(jsx3.gui.Event.KEYUP, me, me.doUnpressed);
            //            jsx3.gui.Event.subscribe(jsx3.gui.Event.MOUSEOUT, me, me.doUnpressed);
//            jsx3.log(this.getName() + " has jsxenabled " + this.jsxenabled);
            if (this.jsxenabled == jsx3.gui.Form.STATEDISABLED) {
//                jsx3.log("Setting disabled");
                this.setDynamicProperty("jsxborder", "@addin@Btn Disabled Border");
                this.setCSSOverride(Button.getDisabledBG());
            }
            else {
//                jsx3.log("Setting enabled");
                this.setDynamicProperty("jsxborder", "@addin@Btn Border");
                this.setCSSOverride(Button.getUnpressedBG());
            }
            //            this.repaint();
        } catch(ex) {
            ux.admin.Main.logException(ex);
        }
        return this.jsxsuper(parent);
    }


    /**
    * This function can be used by Edit or Delete buttons (or any other that require ACL permissions to be used).  It
    * registers the subject it should listen to, and configures whether or not it should require owner access rights or
    * just edit access rights.  If owner rights are required, the published message should have a field called,
    * <code>hasOwner</code> that is set to <code>true</code> or <code>false</code>, indicating if the user has owner
    * access rights to the current item this button would act upon.  If edit rights are required, the published message
    * should have a field called, <code>hasEdit</code> that is set to <code>true</code> or <code>false</code>, indicating
    * if the user has edit access rights to the current item this button would act upon.
    *
    * @param subject {String} the subject to listen for messages
    * @param hasOwner {boolean} set to <code>true</code> if owner access rights are required, <code>false</code> if edit
    *                           access rights are sufficient
    */
    Button_prototype.setEditSubject = function(subject,hasOwner) {
        this.editSubject = subject
        this.isEditOwnerBtn = true;
        this.hasEdit = false;
        var me = this;
        if(hasOwner) {
            ux.admin.PubSub.subscribe(subject, me, me.setDeleteDisplay)
        }
        else{
            this.isEditBtn = true
            ux.admin.PubSub.subscribe(subject, me, me.setEditDisplay);
        }
    }

    Button_prototype.setEditDisplay = function(objEvent) {
        if (objEvent.hasEdit == "true") {
            this.hasEdit = true;
            if(!this.isEditmode){
                this.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true)
            }
        }
        else {
            this.isEditmode = false
            this.hasEdit = false;
            this.setDisplay(jsx3.gui.Block.DISPLAYNONE, true)
        }
    }

    Button_prototype.setDeleteDisplay = function(objEvent) {
        if (objEvent.hasOwner == "true") {
            this.hasEdit = true;
            this.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true)
        }
        else {
            this.hasEdit = false;
            this.setDisplay(jsx3.gui.Block.DISPLAYNONE, true)
        }
    }

    Button_prototype.onDestroy = function(objParent) {
        if (this.editSubject) {
            var me = this
            ux.admin.PubSub.unsubscribe(me.editSubject,me)
            this.editSubject = null;
            this.isEditOwnerBtn = false;
            this.hasEdit = false;
        }
        return this.jsxsuper(objParent);
    }

    Button_prototype.setDisplay = function(display, bRepaint) {
        if (this.isEditBtn) {
            if (this.hasEdit) {
                if (display == jsx3.gui.Block.DISPLAYNONE) {
                    this.isEditmode = true;
                }
                else {
                    this.isEditmode = false;
                }
            }
        }
        if ((this.isEditOwnerBtn && this.hasEdit) || !this.isEditOwnerBtn) {
            return this.jsxsuper(display, bRepaint);
        }
        else {
            return this.jsxsuper(jsx3.gui.Block.DISPLAYNONE, true);
        }
    }


});
